package org.cttic.visualization.visualization.topology;

import lombok.extern.slf4j.Slf4j;
import org.cttic.visualization.visualization.topology.vo.TopologyMaintain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class GraphData {

    //end(门架)关系图
    public static HashMap<String, HashMap<String,Integer>> fenceGraph = new HashMap<String, HashMap<String,Integer>>();

    //end(收费站)关系图
    public static HashMap<String, HashMap<String,Integer>> fenceStationGraph = new HashMap<String, HashMap<String,Integer>>();


    // startId_endId
    public static Set<String> startEnd = new HashSet<>();

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public void initGraph() {


        initTopology();

       // log.info("GraphData.startEnd:{}", GraphData.startEnd);
        log.info("拓扑加载完成");


    }


    private void initTopology() {
        String sql = "SELECT * FROM beidou_office_topology where end_type <> '' " +
            " ORDER BY `start` desc ";
        log.info("mysql***执行拓扑关系查询：{}", sql);
        List<TopologyMaintain> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(TopologyMaintain.class));

        for (TopologyMaintain topology : list) {

            GraphData.startEnd.add(topology.getStart()+"_"+topology.getEnd());
            HashMap<String, Integer> stationHashMap = GraphData.fenceStationGraph.get(topology.getStart());
            if (stationHashMap == null) {
                stationHashMap = new HashMap<String, Integer>();
                GraphData.fenceStationGraph.put(topology.getStart(), stationHashMap);
            }
            stationHashMap.put(topology.getEnd(), 1);

            if (topology.getEndType().intValue() == 2) {
                continue;
            }
            HashMap<String, Integer> hashMap = GraphData.fenceGraph.get(topology.getStart());

            if (hashMap == null) {
                hashMap = new HashMap<String, Integer>();
                GraphData.fenceGraph.put(topology.getStart(), hashMap);
            }
            hashMap.put(topology.getEnd(), 1);

        }
    }



}